using System;
using CommandSystem;

namespace RgCore.Modules.Commands.SubPluginManagerCommands
{
    public class SubPluginManagerCommand<T> : ParentCommand, IUsageProvider where T : Attribute, IName
    {
        public SubPluginManagerCommand(string command, SubPluginManager<T> manager)
        {
            Command = command;
            Manager = manager;
            LoadGeneratedCommands();
        }

        public SubPluginManager<T> Manager { get; private set; }

        public override string Command { get; }

        public override string[] Aliases { get; } = Array.Empty<string>();

        public override string Description => $"【{Manager.Name}】子项目控制命令";

        public string[] Usage => new[]
        {
            "load/reload/unload/list",
            "文件名(load用)/类名(reload, unload用)"
        };

        public string GetUsage() => $"用法: {Command} <load/reload/unload/list> <文件名(load用)/类名(reload, unload用)>";

        public override void LoadGeneratedCommands()
        {
            this.RegisterCommand(new ListCommand<T>(Command, Manager));
            this.RegisterCommand(new Load<T>(Command, Manager));
            this.RegisterCommand(new Reload<T>(Command, Manager));
            this.RegisterCommand(new Unload<T>(Command, Manager));
        }

        protected override bool ExecuteParent(ArraySegment<string> arguments, ICommandSender sender,
            out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ServerConsoleCommands))
            {
                response = "你没有权限执行此命令";
                return false;
            }

            response = GetUsage();
            return false;
        }
    }
}